define(
    function (require) {
        require('tpl!./list.tpl.html');
        var util = require('common/util');
        var globalData = require('common/GlobalData').getInstance();

        var showid = 1;
        var page = 1;

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 20,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '标题',
                field: 'title' ,
                resizable : true,
                width: 120,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="modify">' + item.title + '</a>';
                }
            },
            {
                title: '详细解释',
                field: 'content ' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.content ;
                }
            },
            {
                title: '电子券个数',
                field: 'total' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.total ;
                }
            },
            {
                title: '现价',
                field: 'price' ,
                width: 100,
                content: function (item) {
                    return monydate(item.price);
                }
            },
            {
                title: '原价',
                field: 'oldprice' ,
                width: 100,
                content: function (item) {
                    return monydate(item.oldprice);
                }
            },
            {
                title: '有效时间',
                field: 'expirationTime' ,
                width: 100,
                content: function (item) {
                    return textTime(item.expirationTime);
                }
            },
            {
                title: '是否有效',
                field: 'expirationTime' ,
                width: 100,
                content: function (item) {
                    return judeTime(item.expirationTime);
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 60,
                content: function (item) {
                    var operates = [
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="modify">修改</a></span>',
                        '<span class="list-link" data-command-args="'
                            + item.id
                            + '" data-command="delete">删除</a></span>'
                    ];
                    return operates.join('');
                }
            }
        ];

        function search() {
            page = 1;
            var dashboardName = this.get('dashboardName');
            if(dashboardName.getValue().length>0){
                this.fire('search', { title: dashboardName.getValue() });
            }else{
                alert("检索项不能为空！");
            }
        }

        //时间戳转换
        function textTime(obj) {
            // var time=obj;
            if(obj !=null){
                var seperator1 = "-";
                var year=obj.substring(0,4);
                var month=obj.substring(4,6);
                var dd=obj.substring(6,8);
                var currentdate=year+seperator1+month+seperator1+dd;
                return currentdate;
            }
            return "--";
        }
        //mony转换
        function monydate(obj) {
            var money=parseFloat(obj).toLocaleString()
            return money;
        }
        //是否过期
        function judeTime(obj) {
            var seperator1 = "/";
            var year=obj.substring(0,4);
            var month=obj.substring(4,6);
            var dd=obj.substring(6,8);
            var currentdate=year+seperator1+month+seperator1+dd;
            //时间
            var date1=new Date(currentdate);
            //现在时间
            var date2=new Date();
            if (date2 > date1) {
                return "已失效";
            }
            return "未失效";
        }
        //分页
        function flip(e) {
             var page = e.target.getAttribute('data-page');
            if (/(\+|\-)/.test(page)) {
                page = parseInt(page) + (RegExp.$1 === '+' ? 1 : -1);
            }
            this.fire('flip', { page: page });
        }

        var BaseListView = require('common/ListView');

        function DashboardtListView() {
            BaseListView.apply(this, arguments);
        }

        DashboardtListView.prototype.template = 'dashboardListPage';

        DashboardtListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            var str = location.href;
            var num = str.indexOf("=");
            if(num === -1){
                page = 1;
            }else {
                var name = str.substring(num-4, num);
                if(name === 'page'){
                    page = str.substring(num + 1, str.length);
                }else {
                    page = 1;
                }
            }
            showid = (page-1)*10+1;
            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无优惠信息!</p>'
                }
            };
        }


        DashboardtListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );

            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );

            $('#list-page').on(
                'click',
                ':not(.disable)' ,
                util.bind(flip, this)
            );

            this.get('list').initChildren();
        }

        require('er/util').inherits(DashboardtListView, BaseListView);
        return DashboardtListView;
    }
);
